package com.gdpu.pbss.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gdpu.pbss.Constants.Constants;
import com.gdpu.pbss.DTO.ManagerDTO;
import com.gdpu.pbss.common.HoneyLogs;
import com.gdpu.pbss.common.LogType;
import com.gdpu.pbss.common.Result;
import com.gdpu.pbss.pojo.Manager;
import com.gdpu.pbss.pojo.User;
import com.gdpu.pbss.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;

@RestController
@RequestMapping("/manager")
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    @HoneyLogs(operation = "登录",type = LogType.LOGIN)
    @PostMapping("/login")
    public Result manageLogin(@RequestBody ManagerDTO managerdto){
        String mn=managerdto.getManageName();
        String mp=managerdto.getManagePassword();
        if (StrUtil.isBlank(mn)||StrUtil.isBlank(mp)){
            return Result.error(Constants.CODE_400,"参数错误");
        }

        StpUtil.login(10001);

        ManagerDTO m = managerService.login(managerdto);

        return Result.success(m);
    }

    @HoneyLogs(operation = "注册",type = LogType.REGISTER)
    @PostMapping("/register")
    public Result manageRegister(@RequestBody ManagerDTO managerdto){
        String mn=managerdto.getManageName();
        String mp=managerdto.getManagePassword();
        if (StrUtil.isBlank(mn)||StrUtil.isBlank(mp)){
            return Result.error(Constants.CODE_400,"参数错误");
        }
        return Result.success(managerService.register(managerdto));
    }

    /*
     * 新增或修改*/
    @PostMapping
    public Result saveManager(@RequestBody Manager manager){

        if (manager.getManageId()==null){
            return Result.success(managerService.save(manager));
        }else {
            return Result.success(managerService.updateById(manager));
        }



    }
    @GetMapping("/managerName/{manageName}")
    public Result getManager(@PathVariable String manageName){
        QueryWrapper<Manager> qw=new QueryWrapper<>();
        qw.eq("manage_name",manageName);
      return Result.success(managerService.getOne(qw))  ;

    }

    @GetMapping("/managerId/{manageId}")
    public Result getByID(@PathVariable Integer manageId){
        return Result.success(managerService.getById(manageId));
    }
}
